# -*- coding: utf-8 -*-
import os

from utils.utils import *

dict = {
    "A":"101",
    "B":"102",
    "C":"103",
    "D":"104",
    "E":"105",
    "F":"106",
    "G":"107",
    "H":"108",
    "I":"109",
    "J":"1010",
    "K":"1011",
    "L":"1012",
    "M":"1013",
    "N":"1014",
    "O":"1015",
    "P":"1016",
    "Q":"1017",
    "R":"1018",
    "S":"1019",
    "T":"1020",
    "U":"1021",
    "V":"1022",
    "W":"1023",
    "X":"1024",
    "Y":"1025",
    "Z":"1026",
}


def train_image(path):
    """"""
    for folder_name in os.listdir(path):
        folder_path = "{0}/{1}".format(path, folder_name)

        if os.path.isfile(folder_path):
            continue

        for file_name in os.listdir(folder_path):
            file_path = "{0}/{1}".format(folder_path, file_name)

            if os.path.isdir(file_path) or not file_path.endswith("jpeg"):
                continue

            with open("train_file", "a") as train_file:
                image = Image.open(file_path)
                width = image.width
                height = image.height

                table = get_bin_table(100)
                image = image.point(table, "1")

                if folder_name.isdigit():
                    train_file.write(folder_name + " ")
                else:
                    train_file.write(dict[folder_name] + " ")

                svm_result = ""
                # 记录y
                for y in range(11):  # 此处11是图片最大宽高
                    count = 0
                    if y >= height:
                        svm_result += "{0}:{1} ".format(y + 1, count)
                        continue
                    for x in range(11):
                        if x >= width:
                            continue
                        pixel = image.getpixel((x, y))
                        if pixel == 0:
                            count += 1
                    svm_result += "{0}:{1} ".format(y + 1, count)

                # 记录x
                for x in range(11):
                    count = 0
                    if x >= width:
                        svm_result += "{0}:{1} ".format(x + 12, count)
                        continue
                    for y in range(11):
                        if y >= height:
                            continue
                        pixel = image.getpixel((x, y))
                        if pixel == 0:
                            count += 1
                    svm_result += "{0}:{1} ".format(x + 12, count)

                train_file.write(svm_result.strip() + "\n")


class svm():
    """计算训练图片的宽高"""  # 11，11

    def __init__(self):
        self.max_width = 0
        self.max_height = 0

    def max_margin(self, path):
        """"""
        for file_name in os.listdir(path):
            file_path = path + "/" + file_name
            if os.path.isdir(file_path):
                self.max_margin(file_path)
            if file_path.endswith("jpeg"):
                image = Image.open(file_path)
                print(image.size)
                if image.width > self.max_width:
                    self.max_width = image.width
                if image.height > self.max_height:
                    self.max_height = image.height


train_image("/home/joe/Desktop/results")
